﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace IMT
{
    /// <summary>
    /// Author: Elliott Perry 01/09/2011
    /// 
    /// Parent Class of all messages which deals with 
    ///     -Sending (Done 01/09/2011)
    ///     -Saving (Done 01/09/2011)
    ///     -Updating IBS
    ///     -Generating the XML? 
    ///     -Attaching Docs to XML
    ///     -After being alerted to a new document, XMLMessage should determine type of doc and read in data
    /// </summary>
    class XMLMessage
    {


        public string savePath;

        /// <summary>
        /// This method saves the XML data into an XML file in the correct location.
        /// </summary>
        /// <param name="data"></param>
        public void saveXML(string data)
        {
            try
            {

                string currentSavePath = GlobalData.XMLMsgPath + @"MSG " + getTimeStamp() + ".XML";

                Debug.Write("DEBUG: Trying to save to " + currentSavePath);

                TextWriter writer = new StreamWriter(currentSavePath);

                //Tidy up
                writer.Write(data);
                writer.Close();

                
                //update savePath variable so that this data can be referenced when sending to LS
                savePath = currentSavePath;

                Debug.WriteLine(".... Save Successful");
            }
            catch (Exception e)
            {
                Debug.WriteLine("\nERROR: SaveXML (XMLMessage) Problem Saving File: " + e);
            }

        }
        

        /// <summary>
        /// This method calls the Lime Street web-service, sends the message.
        /// 
        ///     to-Do
        ///        -Deal with the return message from LS.
        /// </summary>
        /// <param name="XMLPath"></param>
        public void sendToLSTEST(string XMLPath)
        {
            LSTest.Placing LSConnector = new LSTest.Placing();

            //Send the message onto Lime Street and return the status of the message into the string
            string responseFromLS = LSConnector.SubmitEndorsementXml(XMLPath).ToString();

        }


        /// <summary>
        /// This method returns the current time in a timestamp so that it can be used for in file names and log files.
        /// </summary>
        /// <returns></returns>
        public string getTimeStamp()
        {

            string timeStamp = DateTime.Now.ToString("yyyy MM dd hh mm ss");

            return timeStamp;

        }

        
    }
}
